package com.ssb.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.ssb.service.check.CheckedFrontSysService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;


/**
 * 创建的校验前端系统插件
 * @Author IUS platform administrators create generation
 * @Date 2020-1-31 13:51:04
 */
public class CheckFrontSysIsExistPluginImpl implements CustomMethodPlugin {
	private Logger log = Logger.getLogger(getClass());

	@Autowired
	private CheckedFrontSysService checkedFrontSysService;

	@Override
	public Object beforeInvoke(Map<String, Object> param) {

		log.info("start->> 执行插件事前方法....");

		//校验此前端系统是否存在且有效
		String frontSysCode = StrUtil.doNull(String.valueOf(param.get("frontSysCode")));
		Map<String, String> result = new HashMap<String, String>();
		result.put("errorCode", "F");
		result.put("errorMsg", "系统异常!");
		//1、校验 frontSysCode 是否为空
		if (Validator.isNull(frontSysCode)){
			result.put("errorCode", "F");
			result.put("errorMsg", "前端系统代码不能为空!");
//			return  result;
			throw new BusinessException("前端系统代码不能为空！");
		}

		//根据前端系统代码 frontSysCode 判断前端请求时该系统在后台是否存在且有效
		Map selectFrontMap = new HashMap();
		selectFrontMap.put("ssFrontSystem.w_infoId",frontSysCode);
		ResultEntity obj = IusPluginUtil.getInstance().select("ssb", "obj", "s_ssFrontSystem", selectFrontMap);
		if (obj == null || obj.getEntity() == null){
			result.put("errorCode", "F");
			result.put("errorMsg", "没有该前端系统的信息，请确认后再继续操作！");
			throw new BusinessException("没有该前端系统的信息，请确认后再继续操作！");
//			return  result;
		}
		DataMap frontMap = (DataMap)obj.getEntity();
		String frontStatus = String.valueOf(frontMap.get("frontStatus"));
		if (frontStatus.equals("0")){
			result.put("errorCode", "F");
			result.put("errorMsg", "该前端系统无效，请确认后再继续操作！");
			throw new BusinessException("该前端系统无效，请确认后再继续操作！");
		}
		String frontApplyBrokerCode = String.valueOf(frontMap.get("frontApplyBrokerCode"));
		result.put("errorCode", "T");
		result.put("errorMsg", "识别成功！");
		result.put("frontApplyBrokerCode", frontApplyBrokerCode);

		log.info("end->> 执行插件事前方法....end");
		return null;
	}

	@Override
	public Object doInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件方法....");

		return null;
	}

	@Override
	public Object afterInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件事后方法....");
		//校验此前端系统是否存在且有效
		String frontSysCode = StrUtil.doNull(String.valueOf(param.get("frontSysCode")));
//		Map<String,String> result =  checkedFrontSysService.checkedSysByFrontSysCode(frontSysCode);
//		if (result.get("errorCode").equals("F")){
//			throw new BusinessException("此系统有误：" + result.get("errorMsg"));
//		}
		Map<String, String> result = new HashMap<String, String>();
		result.put("errorCode", "F");
		result.put("errorMsg", "系统异常!");
		//1、校验 frontSysCode 是否为空
		if (Validator.isNull(frontSysCode)){
			result.put("errorCode", "F");
			result.put("errorMsg", "前端系统代码不能为空!");
//			return  result;
			throw new BusinessException("前端系统代码不能为空！");
		}

		//根据前端系统代码 frontSysCode 判断前端请求时该系统在后台是否存在且有效
		Map selectFrontMap = new HashMap();
		selectFrontMap.put("ssFrontSystem.w_infoId",frontSysCode);
		ResultEntity obj = IusPluginUtil.getInstance().select("ssb", "obj", "s_ssFrontSystem", selectFrontMap);
		if (obj == null || obj.getEntity() == null){
			result.put("errorCode", "F");
			result.put("errorMsg", "没有该前端系统的信息，请确认后再继续操作！");
			throw new BusinessException("没有该前端系统的信息，请确认后再继续操作！");
//			return  result;
		}
		DataMap frontMap = (DataMap)obj.getEntity();
		String frontStatus = String.valueOf(frontMap.get("frontStatus"));
		if (frontStatus.equals("0")){
			result.put("errorCode", "F");
			result.put("errorMsg", "该前端系统无效，请确认后再继续操作！");
			throw new BusinessException("该前端系统无效，请确认后再继续操作！");
		}
		String frontApplyBrokerCode = String.valueOf(frontMap.get("frontApplyBrokerCode"));
		result.put("errorCode", "T");
		result.put("errorMsg", "识别成功！");
		result.put("frontApplyBrokerCode", frontApplyBrokerCode);
		return null;
	}

	/**
	 * 规则校验统一入口
	 * @param paramsVo
	 */
	public void run(ParamsVo paramsVo) {
		beforeInvoke(paramsVo.getParams());
	}


}

